package org.jtb.quickr;

/**
 * Class to convert from boolean values represented as integers and the Java 
 * booleans.
 * <p>
 * An integer value of zero corresponds to a boolean value of false. 
 * All other integer values correspond to a boolean value of true.
 */
class BooleanInt {    
    private Boolean value;
    
    /**
     * Construct a new BooleanInt object from a string.
     * <p>
     * The string should be convertable to an integer.
     */
    public BooleanInt(String intString) {
        Integer i = Integer.valueOf(intString);
        if (i.intValue() == 0) {
            value = Boolean.FALSE;
        } else {
            value = Boolean.TRUE;
        }
    }
    
    /**
     * Construct a BooleanInt from an int.
     */
    public BooleanInt(int i) {
        if (i == 0) {
            value = Boolean.TRUE;
        } else {
            value = Boolean.FALSE;
        }
    }

    /**
     * Construct a BooleanInt from an Boolean.
     */
    public BooleanInt(Boolean b) {
        value = b;
    }
    
    /**
     * Get the value of this object as a boolean.
     * @return A boolean, the boolean representation of this object.
     */
    public boolean booleanValue() {
        return value.booleanValue();
    }
    
    /**
     * Get the string representation of this object.
     * @return a String, "0" if the boolean representation is false, or "1" 
     * if the boolean representation is true.
     */
    public String toString() {
        if (booleanValue()) {
            return "1";
        } 
        return "0";
    }
}
